package com.jjrwa.action;

import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;

public class IVoIdInspection extends LocalInspectionTool {

    @Override
    public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
        return new JavaElementVisitor() {
            @Override
            public void visitClass(PsiClass aClass) {
                super.visitClass(aClass);

                if (aClass.isInterface() || aClass.isEnum() || aClass.isAnnotationType()) {
                    return;
                }

                // 只检查当前打开的类（PSI 自动只分析当前文件，不会全局扫描）
                if (implementsIVO(aClass)) {
                    // 检查 @Id 字段
                    if (!hasIdField(aClass)) {
                        PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
                        if (nameIdentifier != null) {
                            holder.registerProblem(
                                    nameIdentifier,
                                    "类实现IVo，必须要有 @Id字段",
                                    ProblemHighlightType.ERROR,
                                    new AddIdFieldQuickFix() // 快速修复 AddIdField
                            );
                        }
                    }

                    // 检查无参构造函数
                    if (!hasNoArgsConstructor(aClass)) {
                        PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
                        if (nameIdentifier != null) {
                            holder.registerProblem(
                                    nameIdentifier,
                                    "类实现IVo，必须要有无参构造 @NoArgsConstructor",
                                    ProblemHighlightType.ERROR,
                                    new AddNoArgsConstructorQuickFix() // 快速修复 AddNoArgsConstructor
                            );
                        }
                    }
                }
            }

            private boolean implementsIVO(PsiClass psiClass) {
                PsiClassType[] interfaces = psiClass.getImplementsListTypes();
                for (PsiClassType iface : interfaces) {
                    String ifaceName = iface.rawType().getCanonicalText();
                    if ("com.zh.fsc.provide.basics.define.IVo".equals(ifaceName)) {
                        return true;
                    }
                }
                return false;
            }

            private boolean hasIdField(PsiClass psiClass) {
                for (PsiField field : psiClass.getFields()) {
                    if (field.hasAnnotation("javax.persistence.Id")) {
                        return true;
                    }
                }
                return false;
            }

            // 检查类是否有无参构造函数
            private boolean hasNoArgsConstructor(PsiClass psiClass) {
                for (PsiMethod method : psiClass.getConstructors()) {
                    if (method.getParameterList().getParametersCount() == 0) {
                        return true;
                    }
                }
                return false;
            }
        };
    }
}
